package com.company.thread;


import cn.hutool.core.thread.RejectPolicy;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Slf4j
public class Thread20 {


    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(8,16, 200,TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<Runnable>(4),RejectPolicy.ABORT.getValue());


        for (int i = 0; i < 20; i++) {
            MyTask task = new MyTask(i);
            executor.execute(task);
            log.info("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
                    executor.getQueue().size()+"，已执行完任务数目："+executor.getCompletedTaskCount());
        }
        executor.shutdown();
    }
}
@Slf4j(topic = "sub")
class MyTask implements Runnable {
    private int taskNum;

    public MyTask(int num) {
        this.taskNum = num;
    }

    @Override
    public void run() {
        log.info("正在执行task "+taskNum);
        try {
            Thread.currentThread().sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("task "+taskNum+"执行完毕");
    }
}